package com.mimiwang.school.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mimiwang.school.VO.PageConfig;
import com.mimiwang.school.VO.SchoolCampusVo;
import com.mimiwang.school.VO.SchoolVo;
import com.mimiwang.school.domain.School;
import com.mimiwang.school.domain.SchoolArea;
import com.mimiwang.school.domain.SchoolCampus;
import com.mimiwang.school.mapper.SchoolAreaMapper;
import com.mimiwang.school.mapper.SchoolCampusMapper;
import com.mimiwang.school.mapper.SchoolMapper;
import com.mimiwang.school.service.ISchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 学校Service业务层处理
 * 
 * @author ylb
 * @date 2023-04-16
 */
@Service
public class SchoolServiceImpl  implements ISchoolService
{
    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private SchoolCampusMapper schoolCampusMapper;

    @Autowired
    private SchoolAreaMapper schoolAreaMapper;

    @Override
    public PageInfo<School> selectSchoolByProvinceIdAndCityId(long provinceId, long cityId, PageConfig pageConfig) {
        PageHelper.startPage(pageConfig.getPageNum(),pageConfig.getPageSize());

        List<School> schools = schoolMapper.selectAllSchoolByProvinceIdAndCityId(provinceId, cityId);
        PageInfo<School> schoolVoPageInfo = new PageInfo<>(schools);
        return schoolVoPageInfo;


    }





    @Override
    public School selectSchoolBySchoolId(long schoolId) {
        return schoolMapper.selectSchoolBySchoolId(schoolId);
    }

    @Override
    public SchoolVo selectSchoolAndCampusBySchoolId(long schoolId) {

//        创建学校vo类
        SchoolVo schoolVo = new SchoolVo();

//        查询学校
        School school = this.selectSchoolBySchoolId(schoolId);
        schoolVo.setSchool(school);


//        查询学校校区
        List<SchoolCampus> schoolCampuses = schoolCampusMapper.selectCampusListBySchoolId(schoolId);
        List<SchoolCampusVo> lst = new ArrayList<>();
        schoolVo.setSchoolCampusVoList(lst);

//        将 【校区对象】 转换成 【校区vo】 对象
        for (SchoolCampus schoolCampus : schoolCampuses) {
            SchoolCampusVo schoolCampusVo = new SchoolCampusVo();
            schoolCampusVo.setSchoolCampus(schoolCampus);
            lst.add(schoolCampusVo);
        }

        return schoolVo;

    }

    @Override
    public SchoolVo selectSchoolAndCampusAndAreaBySchoolId(long schoolId){
//        获取学校+校区 vo
        SchoolVo schoolVo = this.selectSchoolAndCampusBySchoolId(schoolId);
        List<SchoolCampusVo> schoolCampusVoList = schoolVo.getSchoolCampusVoList();


//        遍历 校区 获得 区域
        for (SchoolCampusVo schoolCampusVo : schoolCampusVoList) {
//            获取校区id
            Long campusId = schoolCampusVo.getSchoolCampus().getId();

//            获取校区List
            List<SchoolArea> schoolAreas = schoolAreaMapper.selectSchoolAreaListByCampusId(campusId);
            schoolCampusVo.setAreaList(schoolAreas);
        }
        return schoolVo;
    }

    @Override
    public PageInfo<School> selectAllSchools(PageConfig pageConfig) {
        PageHelper.startPage(pageConfig.getPageNum(),pageConfig.getPageSize());



        PageInfo<School> schoolVoPageInfo = new PageInfo<>();
        return schoolVoPageInfo;
    }

    @Override
    public List<School> selectSchoolListByLike(String like) {

        return schoolMapper.selectSchoolListByLike(like);
    }

    /**
     * 新增学校
     * 
     * @param school 学校
     * @return 结果
     */
    @Override
    public int insertScSchool(School school)
    {

        return schoolMapper.insertScSchool(school);
    }

    /**
     * 修改学校
     * 
     * @param school 学校
     * @return 结果
     */
    @Override
    public int updateScSchool(School school)
    {
        return schoolMapper.updateScSchool(school);
    }

    /**
     * 批量删除学校
     * 
     * @param ids 需要删除的学校主键
     * @return 结果
     */
    @Override
    public int deleteScSchoolByIds(String[] ids)
    {
        return schoolMapper.deleteScSchoolByIds(ids);
    }

    /**
     * 删除学校信息
     * 
     * @param id 学校主键
     * @return 结果
     */
    @Override
    public int deleteScSchoolById(String id)
    {
        return schoolMapper.deleteScSchoolById(id);
    }
}
